#include <iostream>
#include <fstream>
#include "contacts.pb.h"
using namespace std;

void PrintContacts(contacts2::Contacts& contacts) {
    for(int i = 0; i < contacts.contacts_size(); i++) {
        cout << "--------------联系人" << i + 1 << "的相关信息--------------" << endl;
        const contacts2::PersonInfo& people = contacts.contacts(i);
        cout << "联系人的姓名：" << people.name() << endl;
        cout << "联系人的年龄：" << people.age() << endl;
        for(int j = 0; j < people.phone_size(); j++) {
            const contacts2::PersonInfo_Phone& phone = people.phone(j);
            cout << "联系人的电话" << j + 1 << ": " << phone.number() << " (" << phone.PhoneType_Name(phone.type())  << ")" << endl;
        }

        // 打印地址信息
        // 判断是否有data数据并且数据类型需要是Address
        if(people.has_data() && people.data().Is<contacts2::Address>()) {
            contacts2::Address address;
            // data -> address
            people.data().UnpackTo(&address);
            if(!address.home_address().empty()) {
                cout << "联系人家庭地址：" << address.home_address() << endl;
            }
            if(!address.uint_address().empty()) {
                cout << "联系人单位地址：" << address.uint_address()<< endl;
            }
        }

        // 打印其他联系方式
        // 
        // if(people.has_qq()) {
        // } else if(people.has_wechat()) {
        // }
        switch(people.other_contact_case()) {
            case contacts2::PersonInfo::OtherContactCase::kQq:
                cout << "联系人qq：" << people.qq() << endl;
                break;
            case contacts2::PersonInfo::OtherContactCase::kWechat:
                cout << "联系人微信号：" << people.wechat() << endl;
                break;
            default:
                break;
        }

        // 打印备注信息
        if(people.remark_size()) {
            cout << "备注信息：" << endl;
        }
        for(auto it = people.remark().begin(); it != people.remark().end(); it++) {
            cout << "    " << it->first << ": " << it->second << endl;
        }
    }
}

int main() {
    contacts2::Contacts contacts;
    // 读取文件
    fstream input("contacts.bin", ios::in | ios::binary);
    if(!contacts.ParseFromIstream(&input)) {
        cerr << "parse error!" << endl;
        input.close();
        return -1;
    }

    PrintContacts(contacts);
    
    return 0;
}